local AutoConfirmLoot = LibStub("AceAddon-3.0"):NewAddon("AutoConfirmLoot", "AceConsole-3.0", "AceEvent-3.0", "AceConsole-3.0")

local L = LibStub("AceLocale-3.0"):GetLocale("AutoConfirmLoot", true)

local options = {
	name = L["AutoConfirmLoot"],
	handler = AutoConfirmLoot,
	type = "group",
	args = {
	  c = {
	    name = L["current_settings"],
	    type = "group",
	    inline = true,
	    args = {
		cbop = {
			name = L["bop"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 10,
		},
		cneed = {
			name = L["need"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 20,
		},
		cgreed = {
			name = L["greed"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 30,
		},
		cdiss = {
			name = L["diss"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 40,
		},
		cautoloot = {
			name = L["autoloot"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 50,
		},
--		b0 = {
--			name = L["check_all"],
--			type = execute,
--			func = SetOptionAllC,
--			order = 5,
--		},
	    },
	},
	  d = {
	    name = L["default_settings"],
	    type = "group",
	    inline = true,
	    args = {
		dload = {
			name = L["Use_Defs"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 70,
		},
		dbop = {
			name = L["bop"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 80,
		},
		dneed = {
			name = L["need"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 90,
		},
		dgreed = {
			name = L["greed"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 100,
		},
		ddiss = {
			name = L["diss"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 110,
		},
		dautoloot = {
			name = L["autoloot"],
			type = 'toggle',
			set = 'SetOption',
			get = 'GetOption',
			width = "full",
			order = 120,
		},
--		b1 = {
--			name = L["check_all"],
--			type = execute,
--			func = SetOptionAllD,
--			order = 5,
--		},
	    },
	  },
	},
}

local defaults = {
	char = {
		cbop = true,
		cneed = true,
		cgreed = true,
		cdiss = true,
		cautoloot = true,
		dload = false,
		dbop = true,
		dneed = true,
		dgreed = true,
		ddiss = true,
		dautoloot = true,
	},
}


function AutoConfirmLoot:OnInitialize()
	AutoConfirmLoot.db = LibStub("AceDB-3.0"):New("AutoConfirmLootDB", defaults)
	AutoConfirmLoot.version = GetAddOnMetadata("AutoConfirmLoot", "Version");
	LibStub("AceConfig-3.0"):RegisterOptionsTable("AutoConfirmLoot", options)
	AutoConfirmLoot.optionsDialog = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("AutoConfirmLoot")
end

function AutoConfirmLoot:OnEnable()
	AutoConfirmLoot:Print("Version " .. AutoConfirmLoot.version)
	if AutoConfirmLoot.db.char.dload then
		AutoConfirmLoot.db.char.cbop = AutoConfirmLoot.db.char.dbop
		AutoConfirmLoot.db.char.cneed = AutoConfirmLoot.db.char.dneed
		AutoConfirmLoot.db.char.cgreed = AutoConfirmLoot.db.char.dgreed
		AutoConfirmLoot.db.char.cdiss = AutoConfirmLoot.db.char.ddiss
		AutoConfirmLoot.db.char.cautoloot = AutoConfirmLoot.db.char.dautoloot
		AutoConfirmLoot:Print(L["default_loaded"])
	end
	AutoConfirmLoot:RegisterEvent("CONFIRM_LOOT_ROLL", "CONFIRM_ROLL")
	AutoConfirmLoot:RegisterEvent("CONFIRM_DISENCHANT_ROLL", "CONFIRM_ROLL")
	AutoConfirmLoot:RegisterEvent("LOOT_OPENED")
	AutoConfirmLoot:RegisterChatCommand("autoconfirmloot", "OnSlashCommand")
	AutoConfirmLoot:RegisterChatCommand("acl", "OnSlashCommand")
end

function AutoConfirmLoot:OnDisable()
	AutoConfirmLoot:UnregisterEvent("CONFIRM_LOOT_ROLL")
	AutoConfirmLoot:UnregisterEvent("CONFIRM_DISENCHANT_ROLL")
	AutoConfirmLoot:UnregisterChatCommand("autoconfirmloot")
	AutoConfirmLoot:UnregisterChatCommand("acl")
end

-- slash command

function AutoConfirmLoot:OnSlashCommand(input)
	InterfaceOptionsFrame_OpenToCategory(AutoConfirmLoot.optionsDialog)
end

-- options

function AutoConfirmLoot:GetOption(info)
	return AutoConfirmLoot.db.char[info[#info]]
end

function AutoConfirmLoot:SetOption(info, input)
	AutoConfirmLoot.db.char[info[#info]] = input
end

-- confirm bop

local AutoConfirmLoot_WOW_LootSlot = LootSlot
local function AutoConfirmLoot_LootSlot(slot)
	AutoConfirmLoot_WOW_LootSlot(slot)
	if AutoConfirmLoot.db.char.cbop then
		ConfirmLootSlot(slot)
	end
end
LootSlot = AutoConfirmLoot_LootSlot

-- roll handling

function AutoConfirmLoot:CONFIRM_ROLL(event, rollId, roll)
	if (roll == 1 and AutoConfirmLoot.db.char.cneed) or (roll == 2 and AutoConfirmLoot.db.char.cgreed) or (roll == 3 and AutoConfirmLoot.db.char.cdiss) then
		ConfirmLootRoll(rollId, roll)
	end
end

-- auto looting

function AutoConfirmLoot:LOOT_OPENED(event, blizzardLoot)
	if blizzardLoot == 0 and AutoConfirmLoot.db.char.cautoloot then
		cnt = GetNumLootItems()
		if cnt == 0 then
			CloseLoot()
		else
			for slot = 1, cnt do
				LootSlot(slot)
			end
		end
	end
end

